﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace CourseRecommend
{
    public partial class AddForm : Form
    {
        private string subject;
        public string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Catalog.mdf;Integrated Security=True;User Instance=True";
        public AddForm()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            subject = SubjectBox.Text;
            //open connection
            SqlConnection myConnection = new SqlConnection(connectionString);

            try
            {
                myConnection.Open();
             //   MessageBox.Show("connected");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            //populate first list box with selected subject

            SqlCommand command = new SqlCommand("SELECT * FROM [CatalogTable] WHERE Subject =  @SUBJ", myConnection);
            command.Parameters.AddWithValue("@SUBJ", subject);
            SqlDataReader reader = null;
            reader = command.ExecuteReader();
            bool moreResults = true;
            while (moreResults)
            {
                while (reader.Read())
                {
                    listBox1.Items.Add(reader["Name"].ToString());
                    listBox1.Items.Add(reader["ClassId"].ToString());
                }
                moreResults = reader.NextResult();
            }
            myConnection.Close(); //close connection
        }

        private void AddForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'catalogDataSet.CatalogTable' table. You can move, or remove it, as needed.
            this.catalogTableTableAdapter.Fill(this.catalogDataSet.CatalogTable);

        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            //open connection
            SqlConnection myConnection = new SqlConnection(connectionString);

            try
            {
                myConnection.Open();
                //MessageBox.Show("connected");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            foreach (object i in listBox2.Items)
            {
                string itemToAdd = i.ToString();
                int item = Convert.ToInt16(itemToAdd);
                SqlCommand command = new SqlCommand("INSERT INTO [StudentClassRecords] (RecordsStudentID, RecordsClassId) VALUES (@ID, @CLASS)", myConnection);
                command.Parameters.AddWithValue("@ID", LoginForm.loginName);
                command.Parameters.AddWithValue("@CLASS", item);
                try
                {

                    command.ExecuteNonQuery();
                    command.Parameters.Clear();
                }
                catch(Exception exc)
                    {
                        MessageBox.Show("This class is already in your records");
                    }

            }
            myConnection.Close();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //if (listBox1.SelectedItem != null)
            //{
            //    listBox2.Items.Add(listBox1.SelectedItem.ToString());

            //}
        }

        private void SubjectBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            

        }

        private void FinishButton_Click(object sender, EventArgs e)
        {
            Form HomeForm = new HomeForm();
            HomeForm.Show();
            HomeForm.Activate();
            this.Close(); 
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                try
                {
                    listBox1.DoDragDrop(listBox1.SelectedItem.ToString(), DragDropEffects.Move | DragDropEffects.Copy);
                    //MessageBox.Show(listBox1.SelectedItem.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }

        private void listBox2_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text) == true)
            {
                listBox2.Items.Add(e.Data.GetData(DataFormats.Text));
            }
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            Form HomeForm = new HomeForm();
            HomeForm.Show();
            HomeForm.Activate();
            this.Close();
        }

        private void listBox3_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }

        private void listBox3_QueryAccessibilityHelp(object sender, QueryAccessibilityHelpEventArgs e)
        {

        }

        private void listBox3_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text) == true)
            {
                listBox3.Items.Add(e.Data.GetData(DataFormats.Text));
            }
        }

        private void listBox4_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }

        private void listBox4_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text) == true)
            {
                listBox4.Items.Add(e.Data.GetData(DataFormats.Text));
            }
        }

        private void listBox5_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox5_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }

        private void listBox5_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text) == true)
            {
                listBox5.Items.Add(e.Data.GetData(DataFormats.Text));
            }
        }

        private void listBox6_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }

        private void listBox6_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text) == true)
            {
                listBox6.Items.Add(e.Data.GetData(DataFormats.Text));
            }
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop) == true)
            {
                char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
                string filename ="";
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                foreach (string file in files)
                {
                    listBox1.Items.Add(file);
                    filename = file;
                }
                int counter = 0;
                string line;
                List<string> classes = new List<string>();
                List<string> grades = new List<string>();
                // Read the file and display it line by line.
                System.IO.StreamReader fileToRead = new System.IO.StreamReader(filename);
                while ((line = fileToRead.ReadLine()) != null)
                {
                    string[] words = line.Split(delimiterChars);
                    try
                    {

                        for (int i = 0; i < words.Length; i++)
                        {
                            classes.Add(words[i]);
                            i++;
                            grades.Add(words[i]);
                        }
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.ToString());
                    }
                }
                fileToRead.Close();
                Console.ReadLine();
                //open connection
            SqlConnection myConnection = new SqlConnection(connectionString);

            try
            {
                myConnection.Open();
                //MessageBox.Show("connected");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            for (int i =0; i < classes.Count; i++)
            {
                string itemToAdd = classes[i];
                string gradeToAdd = grades[i];
                int item = Convert.ToInt16(itemToAdd);
                int grade = Convert.ToInt16(gradeToAdd);
                SqlCommand command = new SqlCommand("INSERT INTO [StudentClassRecords] (RecordsStudentID, RecordsClassId, GradeRecieved) VALUES (@ID, @CLASS, @GRADE)", myConnection);
                command.Parameters.AddWithValue("@ID", LoginForm.loginName);
                command.Parameters.AddWithValue("@CLASS", item);
                command.Parameters.AddWithValue("@GRADE", grade);
                try
                {
                    command.ExecuteNonQuery();
                    MessageBox.Show("Classes Added!!");
                }
                catch (Exception dup)
                {
                }
                command.Parameters.Clear();
            }
            myConnection.Close();
        }
            }
        }
    
}
